import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

import QQMapWX from "../static/js/qqmap-wx-jssdk.min.js"

import {getInTheaters} from "../apis"

export default new Vuex.Store({
	// 变量
	state: {
		city: "",
		nowPlayingList:{}
	},
	// 方法
	mutations: {

	},
	// 异步方法 使用dispatch
	actions: {
		getCity(context) {
			uni.showLoading({
				title:"加载中"
			});
			uni.authorize({
				scope:"scope.userLocation",
				success() {
					let qqmapsdk = new QQMapWX({
						key: 'SRVBZ-U3SKO-N4LWF-SZMVO-J2UI2-6WB7E'
					})
					uni.getLocation({
						//type: 'wgs84',
						type: 'gcj02', // 腾讯地图使用国标坐标
						success(res){
							console.log('当前位置的经度：' + res.longitude);
							console.log('当前位置的纬度：' + res.latitude);
							qqmapsdk.reverseGeocoder({
								location:{
									latitude:res.latitude,
									longitude:res.longitude
								},
								success(res){
									//if(res.result.address_component.city)
									let mycity = res.result.address_component.city;
									if(mycity.charAt(mycity.length-1) ==='市'){
										context.state.city = mycity.substr(0,mycity.length-1);
									}
									else{
										context.state.city = mycity;
									}
									console.log(context.state.city)
									getInTheaters({
										start:"0",
										count:"8",
										city:context.state.city
									}).then((res) =>{
										let [error,data] = res;
										context.state.nowPlayingList = data.data;
										uni.hideLoading();
									})
									
								}
							})
						}
						
					})
				},
				fail(res) {
					console.log(res)
				}
			});
		}
	}
})
